package com.bigdata.schaduler.service;

import com.bigdata.schaduler.client.DolphinClient;
import cn.hutool.core.lang.Assert;
import com.bigdata.schaduler.util.PageInfo;
import com.bigdata.schaduler.domain.ProcessDefineParam;
import org.apache.dolphinscheduler.common.enums.HttpMethod;
import org.apache.dolphinscheduler.common.enums.ReleaseState;
import org.apache.dolphinscheduler.dao.entity.DagData;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Optional;

public class ProcessDefineService {

  private final DolphinClient dolphinClient;

  public ProcessDefineService(DolphinClient dolphinClient) {
    this.dolphinClient = dolphinClient;
  }

  public String getProcessDefinitionUri(Long projectCode) {
    return "projects/" + projectCode + "/process-definition";
  }

  public DagData select(Long projectCode, Long code) {
    String uri = getProcessDefinitionUri(projectCode) + "/" + code;
    return dolphinClient.requestFormData(HttpMethod.GET, uri, null, DagData.class);
  }

  public PageInfo<ProcessDefinition> selectPageList(Long projectCode, String searchVal, Integer pageNo, Integer pageSize) {
    pageNo = Optional.ofNullable(pageNo).orElse(1);
    pageSize = Optional.ofNullable(pageSize).orElse(10);

    String uri = getProcessDefinitionUri(projectCode);
    MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
    params.add("searchVal", searchVal);
    params.add("pageNo", pageNo);
    params.add("pageSize", pageSize);
    return dolphinClient.requestFormData(HttpMethod.GET, uri, params, PageInfo.class);
  }

  public void delete(Long projectCode, Long code) {
    String uri = getProcessDefinitionUri(projectCode) + "/" + code;
    dolphinClient.requestFormData(HttpMethod.DELETE, uri, null, Void.class);
  }

  public ProcessDefinition create(Long projectCode, ProcessDefineParam processDefineParam) {
    String uri = getProcessDefinitionUri(projectCode);
    return dolphinClient.requestFormData(HttpMethod.POST, uri, processDefineParam, ProcessDefinition.class);
  }

  public ProcessDefinition update(Long projectCode, DagData dagData) {
    ProcessDefineParam processDefineParam = new ProcessDefineParam();
    processDefineParam.convertDagDataToProcessDefineParam(dagData);
    String uri = getProcessDefinitionUri(projectCode) + "/" + processDefineParam.getCode();
    return dolphinClient.requestFormData(HttpMethod.PUT, uri, processDefineParam, ProcessDefinition.class);
  }

  public ProcessDefinition update(Long projectCode, ProcessDefineParam processDefineParam) {
    String uri = getProcessDefinitionUri(projectCode) + "/" + processDefineParam.getCode();
    return dolphinClient.requestFormData(HttpMethod.PUT, uri, processDefineParam, ProcessDefinition.class);
  }

  public void release(Long projectCode, Long code, ReleaseState releaseState) {
    Assert.notNull(releaseState, "releaseState is null.");
    String uri = getProcessDefinitionUri(projectCode) + "/" + code + "/release?releaseState=" + releaseState.name();
    dolphinClient.requestFormData(HttpMethod.POST, uri, null, Void.class);
  }
}
